package com.myapp.tools.msgbuilder;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.myapp.tools.bean.ToolsConfig;
import com.myapp.tools.msgbuilder.base.MsgBean;
import com.myapp.tools.util.BuilderTools;

public class MsgBuilder {

	private String msgPackage = null;
	private String logicalPackage = null;
	private List<MsgBean> msgs = null;
	private List<MsgBean> allMessage = new ArrayList<MsgBean>();

	public void builderMsg(String msgPackage, String logicalPackage,
			List<MsgBean> msgs) {
		this.msgPackage = msgPackage;
		this.logicalPackage = logicalPackage;
		this.msgs = msgs;
		for (MsgBean msgBean : this.msgs) {
			// 生成服务器端消息
			this.builderServerMsg(msgBean);
			// 生成服务器端业务逻辑
			this.buildServerLogical(msgBean);
			// 生成客户端端消息
			this.builderClientMsg(msgBean);
			// 生成客户端的handler文件
			this.builderClientHandler(msgBean);

			// 记录所有的消息
			allMessage.addAll(msgs);
		}
	}

	/**
	 * 生成客户端的消息文件
	 * @param msgBean
	 */
	private void builderClientMsg(MsgBean msgBean) {
		String templatePath = ToolsConfig.appToolsConfigBean.getBase_Path()
			+ File.separator
			+ ToolsConfig.appToolsConfigBean.getMsg_Client_MsgTemplate();

		String savePath = ToolsConfig.appToolsConfigBean.getBase_Path()
			+ File.separator + "export" + File.separator + "src"
			+ File.separator
			+ "net.message".replaceAll("\\.", "\\" + File.separator);

		String fileName = msgBean.getName() + "Message.as";
		
		// 准备数据
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("MsgPackage", msgPackage);
		root.put("LogicalPackage", logicalPackage);
		root.put("FieldBeanList", msgBean.getFiles());
		root.put("BeanBeanList", msgBean.getBeans());
		root.put("MsgName", msgBean.getName());
		root.put("MsgComment", msgBean.getComment());
		root.put("MsgId", msgBean.getId());
		root.put("MsgPermission", msgBean.getPermission());
		
		// 生成文件
		BuilderTools.build(templatePath, savePath, fileName, root);
	}

	/**
	 * 生成客户端的handler文件
	 * @param msgBean
	 */
	private void builderClientHandler(MsgBean msgBean) {
		String templatePath = ToolsConfig.appToolsConfigBean.getBase_Path()
			+ File.separator
			+ ToolsConfig.appToolsConfigBean.getMsg_Client_HandlerTemplate();

		String savePath = ToolsConfig.appToolsConfigBean.getBase_Path()
			+ File.separator + "export" + File.separator + "src"
			+ File.separator
			+ "net.message".replaceAll("\\.", "\\" + File.separator);

		String fileName = msgBean.getName() + "Handler.as";
		
		// 准备数据
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("MsgPackage", msgPackage);
		root.put("LogicalPackage", logicalPackage);
		root.put("FieldBeanList", msgBean.getFiles());
		root.put("BeanBeanList", msgBean.getBeans());
		root.put("MsgName", msgBean.getName());
		root.put("MsgComment", msgBean.getComment());
		root.put("MsgId", msgBean.getId());
		root.put("MsgPermission", msgBean.getPermission());
		
		// 生成文件
		BuilderTools.build(templatePath, savePath, fileName, root);
	}

	/**
	 * 生成客户端所有消息文件
	 * @param msgBean
	 */
	public void  builderClientAllMessage() {
		String templatePath = ToolsConfig.appToolsConfigBean.getBase_Path()
			+ File.separator
			+ ToolsConfig.appToolsConfigBean.getMsg_Client_AllMessageTemplate();

		String savePath = ToolsConfig.appToolsConfigBean.getBase_Path()
			+ File.separator + "export" + File.separator + "src"
			+ File.separator
			+ "net".replaceAll("\\.", "\\" + File.separator);

		String fileName = "MessageType.as";
		
		List<MsgBean> msgsOrderByType = new ArrayList<MsgBean>();
		
		MsgBean GCBegin = new MsgBean();
		GCBegin.setName("Client->server");
		msgsOrderByType.add(GCBegin);
		
		for (MsgBean msg : allMessage) {
			if (msg.getName().startsWith("GC")) {
				msgsOrderByType.add(msg);
			}
		}

		MsgBean CGBegin = new MsgBean();
		CGBegin.setName("server->Client");
		msgsOrderByType.add(CGBegin);

		for (MsgBean msg : allMessage) {
			if (msg.getName().startsWith("CG")) {
				msgsOrderByType.add(msg);
			}
		}
		
		// 准备数据
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("msgs", msgsOrderByType);
		
		
		// 生成文件
		BuilderTools.build(templatePath, savePath, fileName, root);

	}
	/**
	 * 生成服务器端消息代码
	 * 
	 * @param msgBean
	 *            消息bean
	 */
	private void builderServerMsg(MsgBean msgBean) {
		String templatePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator
				+ ToolsConfig.appToolsConfigBean.getMsg_Server_MsgTemplate();

		String savePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator + "export" + File.separator + "src"
				+ File.separator
				+ this.msgPackage.replaceAll("\\.", "\\" + File.separator);

		String fileName = msgBean.getName() + "Msg.java";

		// 准备数据
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("MsgPackage", msgPackage);
		root.put("LogicalPackage", logicalPackage);
		root.put("FieldBeanList", msgBean.getFiles());
		root.put("BeanBeanList", msgBean.getBeans());
		root.put("MsgName", msgBean.getName());
		root.put("MsgComment", msgBean.getComment());
		root.put("MsgId", msgBean.getId());
		root.put("MsgPermission", msgBean.getPermission());

		// 生成文件
		BuilderTools.build(templatePath, savePath, fileName, root);
	}

	/**
	 * 生成服务器业务逻辑代码
	 * 
	 * @param msgBean
	 *            消息bean
	 */
	public void buildServerLogical(MsgBean msgBean) {
		String templatePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator
				+ ToolsConfig.appToolsConfigBean
						.getMsg_Server_LogicalTemplate();

		String savePath = ToolsConfig.appToolsConfigBean.getBase_Path()
				+ File.separator + "export" + File.separator + "src"
				+ File.separator
				+ this.logicalPackage.replaceAll("\\.", "\\" + File.separator);

		String fileName = msgBean.getName() + "Logical.java";

		// 准备数据
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("MsgPackage", msgPackage);
		root.put("LogicalPackage", logicalPackage);
		root.put("MsgName", msgBean.getName());
		root.put("MsgComment", msgBean.getComment());
		root.put("MsgType", msgBean.getType());
		root.put("MsgPermission", msgBean.getPermission());

		// 生成文件
		BuilderTools.build(templatePath, savePath, fileName, root);
	}
}
